﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using PhotoGallery.DAL;
using PhotoGallery.Model;
namespace PhotoGallery.DLL
{
    public class UserDataAccess
    {
        public userDataContext udc = new userDataContext();
        /*插入新纪录*/
        public void insert(String username, String password, String nickname) 
        {
            user user = new user();
            user.username = username;
            user.password = password;
            user.nickname = nickname;
            user.time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            udc.users.InsertOnSubmit(user);
            udc.SubmitChanges();
        }
        /*删除用户*/
        public void delete(int id) 
        {
            var result = from m in udc.users
                         where m.uid == id
                         select m;
            foreach (user m in result)
                udc.users.DeleteOnSubmit(m);
            udc.SubmitChanges();
        }
        /*按照id搜索用户*/
        public User searchUserById(string uid)
        {   int id=int.Parse(uid);
            var query = udc.users.Where(m => m.uid == id).Select(m => new User(m.uid.ToString(), m.username.ToString(), m.password.ToString(), m.nickname.ToString(), m.time.ToString(), m.role.ToString()));
            foreach (var m in query)
                if (m != null)
                    return m;
                else
                    return null;
            return null;
        }
        /*按照用户名搜索用户*/
        public User searchUserByUsername(string username) 
        {
            var query = udc.users.Where(m => m.username == username).Select(m => new User(m.uid.ToString(), m.username.ToString(), m.password.ToString(), m.nickname.ToString(), m.time.ToString(), m.role.ToString()));
            foreach (var m in query)
                if (m != null)
                    return m;
                else
                    return null;
            return null;
        }
        /*按照昵称搜索用户*/
        public User searchUserByNickname(string nickname) 
        {
            var query = udc.users.Where(m => m.nickname == nickname).Select(m => new User(m.uid.ToString(), m.username.ToString(), m.password.ToString(), m.nickname.ToString(), m.time.ToString(), m.role.ToString()));
            foreach (var m in query)
                if (m != null)
                    return m;
                else
                    return null;
            return null;
        }
        /*修改用户信息*/
        public void modifyUser(string uid,User myuser) 
        {
            var result = from m in udc.users
                         where m.uid == int.Parse(uid)
                         select m;
            foreach (user u in result) {
                u.username = myuser.Username;
                u.password = myuser.Password;
                u.role = int.Parse(myuser.Role);
                u.nickname = myuser.Nickname;
            }
            udc.SubmitChanges();
        }
    }
}
